package net.dethnull.joscontrol.osc;

import javafx.beans.property.SimpleStringProperty;
import net.dethnull.joscontrol.gui.JOSControl;
import net.dethnull.joscontrol.parse.ParseKey;

import java.util.ArrayList;

/**
 * Project: jOSControl
 * Package: net.dethnull.joscontrol.osc
 * Author: dethnull
 * Date: 2/1/13
 * <p/>
 * <p/>
 * Description:
 */
public class Client {

    private final SimpleStringProperty _ipAddress = new SimpleStringProperty();
    private final SimpleStringProperty _addressPattern = new SimpleStringProperty();
    //Textual form of int key
    private final SimpleStringProperty _keyStr = new SimpleStringProperty("null");

    private ArrayList<Object> _arguments = new ArrayList<>();



    Key keyThread = new Key();

    Client() {}

    Client(String ipAddress, String addressPattern, Object arguments) {
        this.set_ipAddress(ipAddress);
        this.set_addressPattern(addressPattern);
        this.set_arguments(arguments);
    }


    public SimpleStringProperty ipAddressProperty() {
        return this._ipAddress;
    }

    public SimpleStringProperty addressPatternProperty() {
        return this._addressPattern;
    }

    public SimpleStringProperty keyValueProperty() {
        return this._keyStr;
    }

    public void set_ipAddress(String ipAddress) {
        this._ipAddress.set(ipAddress);
    }

    public void set_addressPattern(String addressPattern) {
        this._addressPattern.set(addressPattern);
    }

    public void set_arguments(Object argument) {
        this._arguments.add(argument);
    }

    public void set_key(int key) {
        //TODO: parse incoming key
        this.keyThread.set_key(key);
    }

    public void set_key(char letter) {
        JOSControl.addToDebugLog(letter + " was set for address: " + this.addressPatternProperty().get());
        set_key(ParseKey.character2Integer(letter));
        keyValueProperty().set(String.valueOf(letter));
    }

    public int get_keyInt() {
        return this.keyThread.get_key();
    }


    public ArrayList<Object> get_arguments() {
        return this._arguments;
    }

    /**
     * Creates the key thread
     */
    public boolean start() {
        if (keyThread.isInterrupted()) {
            keyThread.reset();
        }

        if (!keyThread.isThreadCreated() && !keyThread.isInterrupted()) {
            //Thread hasn't been started yet, lets start it
            //keyThread.set_key(this.keyValueProperty().get());
            keyThread.setThreadCreated();
            new Thread(keyThread).start();
            keyThread.setName(this.addressPatternProperty().get());

            return true;
        } else {
            //JOSControl.addToDebugLog("Thread has already been created for " + this.addressPatternProperty().get());
            return false;
        }
    }

    public void stop() {
        keyThread.interrupt();
    }

    public void sendKeyPress() {
        this.keyThread.pressKey();
    }

    public void sendKeyRelease() {
        this.keyThread.releaseKey();
    }


}
